﻿namespace Blaze.ConfigExportTool.Serialization
{
    using System;
    using System.IO;
    using InternalSerializer = System.Xml.Serialization.XmlSerializer;

    public class XmlSerializer : SerializerBase
    {
        /// <summary>
        /// 构造一个序列化器，并指定需要序列化的类型。
        /// </summary>
        /// <param name="targetType">目标类型</param>
        public XmlSerializer(Type targetType)
            : base(targetType)
        {
        }

        /// <summary>
        /// 当需要序列化时调用此方法。
        /// </summary>
        /// <param name="target">需要序列化的对象</param>
        protected override void OnSerialize(object target)
        {
            using (var stream = new MemoryStream())
            {
                var internalSerializer = new InternalSerializer(target.GetType());
                internalSerializer.Serialize(stream, target);
                Writer.Write(stream.ToArray());
            }
        }
    }
}